var sd = require('silly-datetime');
const TUtils = require('./utils');

var dataPump = new TUtils();
var inputSrcTm =0;
var postStaticDataTm =0;
var acImport = function(){
    var now = ( new Date()).getTime();
    var timeout_InSrc = ( now - inputSrcTm) > 5*60*1000;
    var timeout_PostStatic = ( now - postStaticDataTm) > 30*60*1000;
    var start =  sd.format(new Date(now - 24*3600*1000), 'YYYY-MM-DD HH:00:00');
    var end =  sd.format(new Date(now + 1*36000*1000), 'YYYY-MM-DD HH:00:00');
    if( timeout_InSrc ){
        dataPump.inputSrcData( function(){
            inputSrcTm = now;
            console.log(sd.format(new Date(), 'YYYY-MM-DD HH:mm:ss') , 'input source datas.');
            if( timeout_PostStatic){
                dataPump.writeStatic2DB(start , end , function(){
                    postStaticDataTm = now;
                    console.log(sd.format(new Date(), 'YYYY-MM-DD HH:mm:ss') ,'post static data.');
                    acImport();
                })
            }
            else{
                acImport();
            }
        })
    }
    else{
        setTimeout(() => {
            acImport();
        }, 2*60*1000);
    }
}

acImport();
 